- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathsource.cpp
281 lines (241 loc) · 9.62 KB
/
source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// <Snippet1>
#using <system.dll>
#using <system.data.dll>
#using <system.drawing.dll>
#using <system.windows.forms.dll>
#using <system.xml.dll>
usingnamespaceSystem;
usingnamespaceSystem::Data;
usingnamespaceSystem::Drawing;
usingnamespaceSystem::Globalization;
usingnamespaceSystem::Windows::Forms;
#definenull0L
public ref classForm1: publicForm
{
private:
System::ComponentModel::Container^ components;
Button^ button1;
Button^ button2;
Button^ button3;
Button^ button4;
TextBox^ text1;
TextBox^ text2;
TextBox^ text3;
BindingManagerBase^ bmCustomers;
BindingManagerBase^ bmOrders;
DataSet^ ds;
DateTimePicker^ DateTimePicker1;
public:
Form1()
{
// Required for Windows Form Designer support.
InitializeComponent();
// Call SetUp to bind the controls.
SetUp();
}
private:
voidInitializeComponent()
{
// Create the form and its controls.
this->components = gcnew System::ComponentModel::Container;
this->button1 = gcnew Button;
this->button2 = gcnew Button;
this->button3 = gcnew Button;
this->button4 = gcnew Button;
this->text1 = gcnew TextBox;
this->text2 = gcnew TextBox;
this->text3 = gcnew TextBox;
this->DateTimePicker1 = gcnew DateTimePicker;
this->Text = "Binding Sample";
this->ClientSize = System::Drawing::Size( 450, 200 );
button1->Location = System::Drawing::Point( 24, 16 );
button1->Size = System::Drawing::Size( 64, 24 );
button1->Text = "<";
button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );
button2->Location = System::Drawing::Point( 90, 16 );
button2->Size = System::Drawing::Size( 64, 24 );
button2->Text = ">";
button2->Click += gcnew System::EventHandler( this, &Form1::button2_Click );
button3->Location = System::Drawing::Point( 90, 100 );
button3->Size = System::Drawing::Size( 64, 24 );
button3->Text = "<";
button3->Click += gcnew System::EventHandler( this, &Form1::button3_Click );
button4->Location = System::Drawing::Point( 150, 100 );
button4->Size = System::Drawing::Size( 64, 24 );
button4->Text = ">";
button4->Click += gcnew System::EventHandler( this, &Form1::button4_Click );
text1->Location = System::Drawing::Point( 24, 50 );
text1->Size = System::Drawing::Size( 150, 24 );
text2->Location = System::Drawing::Point( 190, 50 );
text2->Size = System::Drawing::Size( 150, 24 );
text3->Location = System::Drawing::Point( 290, 150 );
text3->Size = System::Drawing::Size( 150, 24 );
DateTimePicker1->Location = System::Drawing::Point( 90, 150 );
DateTimePicker1->Size = System::Drawing::Size( 200, 800 );
this->Controls->Add( button1 );
this->Controls->Add( button2 );
this->Controls->Add( button3 );
this->Controls->Add( button4 );
this->Controls->Add( text1 );
this->Controls->Add( text2 );
this->Controls->Add( text3 );
this->Controls->Add( DateTimePicker1 );
}
public:
~Form1()
{
if ( components != nullptr )
{
delete components;
}
}
private:
voidSetUp()
{
// Create a DataSet with two tables and one relation.
MakeDataSet();
BindControls();
}
protected:
voidBindControls()
{
/* Create two Binding objects for the first two TextBox
controls. The data-bound property for both controls
is the Text property. The data source is a DataSet
(ds). The data member is the
"TableName.ColumnName" string. */
text1->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custName" ) );
text2->DataBindings->Add( gcnew Binding( "Text",ds,"customers.custID" ) );
/* Bind the DateTimePicker control by adding a new Binding.
The data member of the DateTimePicker is a
TableName.RelationName.ColumnName string. */
DateTimePicker1->DataBindings->Add( gcnew Binding( "Value",ds,"customers.CustToOrders.OrderDate" ) );
/* Add event delegates for the Parse and Format events to a
new Binding object, and add the object to the third
TextBox control's BindingsCollection. The delegates
must be added before adding the Binding to the
collection; otherwise, no formatting occurs until
the Current object of the BindingManagerBase for
the data source changes. */
Binding^ b = gcnew Binding( "Text",ds,"customers.custToOrders.OrderAmount" );
b->Parse += gcnew ConvertEventHandler( this, &Form1::CurrencyStringToDecimal );
b->Format += gcnew ConvertEventHandler( this, &Form1::DecimalToCurrencyString );
text3->DataBindings->Add( b );
// Get the BindingManagerBase for the Customers table.
bmCustomers = this->BindingContext[ ds, "Customers" ];
/* Get the BindingManagerBase for the Orders table using the
RelationName. */
bmOrders = this->BindingContext[ ds, "customers.CustToOrders" ];
}
private:
voidDecimalToCurrencyString( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
/* This method is the Format event handler. Whenever the
control displays a new value, the value is converted from
its native Decimal type to a string. The ToString method
then formats the value as a Currency, by using the
formatting character "c". */
// The application can only convert to string type.
if ( cevent->DesiredType != String::typeid )
return;
cevent->Value = (dynamic_cast<Decimal^>(cevent->Value))->ToString( "c" );
}
voidCurrencyStringToDecimal( Object^ /*sender*/, ConvertEventArgs^ cevent )
{
/* This method is the Parse event handler. The Parse event
occurs whenever the displayed value changes. The static
ToDecimal method of the Convert class converts the
value back to its native Decimal type. */
// Can only convert to Decimal type.
if ( cevent->DesiredType != Decimal::typeid )
return;
cevent->Value = Decimal::Parse( cevent->Value->ToString(), NumberStyles::Currency, nullptr );
/* To see that no precision is lost, print the unformatted
value. For example, changing a value to "10.0001"
causes the control to display "10.00", but the
unformatted value remains "10.0001". */
Console::WriteLine( cevent->Value );
}
private:
voidbutton1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Go to the previous item in the Customer list.
bmCustomers->Position -= 1;
}
voidbutton2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Go to the next item in the Customer list.
bmCustomers->Position += 1;
}
voidbutton3_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Go to the previous item in the Orders list.
bmOrders->Position = bmOrders->Position - 1;
}
voidbutton4_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Go to the next item in the Orders list.
bmOrders->Position = bmOrders->Position + 1;
}
private:
// Create a DataSet with two tables and populate it.
voidMakeDataSet()
{
// Create a DataSet.
ds = gcnew DataSet( "myDataSet" );
// Create two DataTables.
DataTable^ tCust = gcnew DataTable( "Customers" );
DataTable^ tOrders = gcnew DataTable( "Orders" );
// Create two columns, and add them to the first table.
DataColumn^ cCustID = gcnew DataColumn( "CustID",Int32::typeid );
DataColumn^ cCustName = gcnew DataColumn( "CustName" );
tCust->Columns->Add( cCustID );
tCust->Columns->Add( cCustName );
// Create three columns, and add them to the second table.
DataColumn^ cID = gcnew DataColumn( "CustID",Int32::typeid );
DataColumn^ cOrderDate = gcnew DataColumn( "orderDate",DateTime::typeid );
DataColumn^ cOrderAmount = gcnew DataColumn( "OrderAmount",Decimal::typeid );
tOrders->Columns->Add( cOrderAmount );
tOrders->Columns->Add( cID );
tOrders->Columns->Add( cOrderDate );
// Add the tables to the DataSet.
ds->Tables->Add( tCust );
ds->Tables->Add( tOrders );
// Create a DataRelation, and add it to the DataSet.
DataRelation^ dr = gcnew DataRelation( "custToOrders",cCustID,cID );
ds->Relations->Add( dr );
/* Populate the tables. For each customer and order,
create two DataRow variables. */
DataRow^ newRow1; // = new DataRow();
DataRow^ newRow2; // = new DataRow();
// Create three customers in the Customers Table.
for ( int i = 1; i < 4; i++ )
{
newRow1 = tCust->NewRow();
newRow1[ "custID" ] = i;
// Add the row to the Customers table.
tCust->Rows->Add( newRow1 );
}
tCust->Rows[ 0 ][ "custName" ] = "Alpha";
tCust->Rows[ 1 ][ "custName" ] = "Beta";
tCust->Rows[ 2 ][ "custName" ] = "Omega";
// For each customer, create five rows in the Orders table.
for ( int i = 1; i < 4; i++ )
{
for ( int j = 1; j < 6; j++ )
{
newRow2 = tOrders->NewRow();
newRow2[ "CustID" ] = i;
newRow2[ "orderDate" ] = System::DateTime( 2001, i, j * 2 );
newRow2[ "OrderAmount" ] = i * 10 + j * .1;
// Add the row to the Orders table.
tOrders->Rows->Add( newRow2 );
}
}
}
};
intmain()
{
Application::Run( gcnew Form1 );
}
// </Snippet1>